package cn.vinux.vitapollo.common.dto;

import cn.vinux.vitapollo.common.enums.ResCodeEnum;

import java.io.Serializable;
import java.util.Date;

/**
 * @version V3.0
 * @Title: ResultDTO
 * @Company:
 * @Description: 描述
 * @author: alibeibei
 * @date 2018/11/13 下午4:37
 */
public class ResultDto<T> implements Serializable {

    private static final long serialVersionUID = 8500271315123556978L;
    // 响应时间戳
    private Date timestamp;

    // 响应业务状态
    private Integer status;

    // 响应消息
    private String msg;

    // 响应中的数据
    private T data;

    public static ResultDto ok(Object data) {
        return new ResultDto(data);
    }

    public static ResultDto ok() {
        return new ResultDto(null);
    }

    public ResultDto() {

    }

    public static ResultDto build(Integer status, String msg, Object data) {
        return new ResultDto(status, msg, data);
    }

    public static ResultDto build(Integer status, String msg) {
        return new ResultDto(status, msg, null);
    }

    public static ResultDto build(ResCodeEnum resCodeEnum) {
        return new ResultDto(resCodeEnum.getCode(), resCodeEnum.getMsg(), null);
    }

    public ResultDto(Integer status, String msg, T data) {
        this.timestamp = new Date(System.currentTimeMillis());
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ResultDto(T data) {
        this.timestamp = new Date(System.currentTimeMillis());
        this.status = ResCodeEnum.SUCCESS.getCode();
        this.msg = ResCodeEnum.SUCCESS.getMsg();
        this.data = data;
    }

    public boolean checkOk() {
        return this.getStatus() == ResCodeEnum.SUCCESS.getCode();
    }

    public boolean checkErr() {
        return !checkOk();
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    @Override
    public String toString() {
        return "Result{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
